<?php
/**
 * @copyright Arthur Beer
 * @package image
 *
 * @copyright TinyWEB CMS
 * @link http://tinywebcms.ru
 * @link http://cms.vingrad.ru
 * @version SVN: $Id$ 
 */
 
/**
 * ImageResizer
 */
class ImageResizer extends Image implements IExecutable
{
    const CONVERT_PROGRAM   = '/usr/local/bin/convert';
    
    private $filename;
    private $newname;
    private $x;
    private $y;
    
    public function __construct($filename, $x = null, $y = null, $newname = null, $exec = false)
    {
        $this->filename = $filename;
        $this->newname = ($newname) ? $newname : $filename;
        
        // sizes
        $this->x = $x;
        $this->y = $y;
        
        if($exec) {
            $this->exec();
        }
    }
    
    public function exec()
    {
        exec(self::CONVERT_PROGRAM . " -resize '{$this->x}x{$this->y}' $this->filename $this->newname", $output, $code);
        if($code > 0) {
            throw new Exception('Image: can\'t convert image ' . $this->filename . "\n" . implode("\n", array($output, self::CONVERT_PROGRAM . " -resize '{$this->x}x{$this->y}' $this->filename $this->newname")), $code);
        }
    }
    
    public function crop($x, $y, $params = '+0+0+0+0')
    {
        exec(self::CONVERT_PROGRAM . " -crop '{$x}x{$y}$params' -gravity center $this->filename $this->newname", $output, $code);
        if($code > 0) {
            throw new Exception('Image: can\'t crop image ' . $this->filename . "\n" . implode("\n", $output), $code);
        }
    }
    
    public function setFilename($filename)
    {
        $this->filename = $filename;
    }
    
    public function setNewFilename($newname)
    {
        $this->newname = $newname;
    }
    
    public function getFilename()
    {
        return $this->filename;
    }
    
    public function getNewFilename()
    {
        return $this->newname;
    }
}

/**
 * Tab: 4 spaces
 * Encoding: utf-8
 */
?>